package com.snap.awesomeserial.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;

import com.snap.awesomeserial.AppConfig;
import com.snap.awesomeserial.R;
import com.snap.awesomeserial.adapter.DemoAdapter;
import com.snap.awesomeserial.base.BaseActivity;
import com.snap.awesomeserial.bean.TemplateEntity;
import com.snap.awesomeserial.model.DBManager;
import com.snap.awesomeserial.utils.ToastUtils;

import org.litepal.crud.callback.FindMultiCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.demo_rec)
    RecyclerView demoRec;
    @BindView(R.id.fan_btn)
    CheckBox fanCb;

    DemoAdapter demoAdapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_home;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
        fanCb.setChecked(AppConfig.Dynamic.fan);
    }

    private void initViews() {
        fanCb.setChecked(AppConfig.Dynamic.fan);
        fanCb.setOnCheckedChangeListener((buttonView, isChecked) -> AppConfig.Dynamic.fan = isChecked);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        demoRec.setLayoutManager(manager);

        DBManager.get().getAllDemo(new FindMultiCallback<TemplateEntity>() {
            @Override
            public void onFinish(List<TemplateEntity> list) {
                demoAdapter = new DemoAdapter(HomeActivity.this,list);
                demoRec.setAdapter(demoAdapter);
            }
        });
    }

    private void initData() {
        DBManager.get().getAllDemo(new FindMultiCallback<TemplateEntity>() {
            @Override
            public void onFinish(List<TemplateEntity> list) {
                if (demoAdapter!=null){
                    demoAdapter.bindDataList(list);
                }
            }
        });
    }

    @OnClick({R.id.setting_btn, R.id.uv_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_btn:
                Intent pidIntent = new Intent(this, SettingActivity.class);
                startActivity(pidIntent);
                break;
            case R.id.uv_btn:
                ToastUtils.get().showText("开启紫外线杀毒");
                break;
            default:
        }
    }
}
